package com.bblocks.common.anti_reptile.rule.impl;

import com.bblocks.common.anti_reptile.config.AntiReptileProperties;
import com.bblocks.common.anti_reptile.config.bean.RuleComm;
import com.bblocks.common.anti_reptile.constant.RuleTypeEnum;
import com.bblocks.common.anti_reptile.module.IpUserNumVo;
import com.bblocks.common.anti_reptile.module.RuleStatVO;
import com.bblocks.common.anti_reptile.module.RuleVO;
import com.bblocks.common.anti_reptile.rule.RuleData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>Project: bb-anti-reptile - RuleDataDefault</p>
 * <p>描述：提取获取规则 默认实现- 提取规则:maxNum/time</p>
 *
 * @Author Zhao [125043150@qq.com]
 * @Date 2024/3/25 9:15
 * @Version 1.0
 * @since 8
 */
@RequiredArgsConstructor
@Slf4j
public class RuleDataDefault implements RuleData {

    private final AntiReptileProperties antiReptileProperties;

    @Override
    public RuleVO theRule(RuleTypeEnum ruleTypeEnum, String url,String ip) {
        RuleComm rule = antiReptileProperties.ruleByType(ruleTypeEnum);
        if(rule == null){
            return null;
        }
        Integer rateAvg = rule.defaultQps();
        if(rateAvg != null){
            rateAvg = rule.getRequestMaxSize() / rule.getExpirationTime();
            return RuleVO.builder()
                    .type(ruleTypeEnum.getType())
                    .url(url)
                    .rateAvg(rateAvg)
                    .rateMax((int)(rateAvg * rule.getMultiplier() / 100.0))
                    .build();
        }
        return null;
    }

    @Override
    public void saveRule(RuleStatVO ruleStatVO) {

    }

    @Override
    public void saveIpUser(IpUserNumVo userNoVo) {

    }


}
